jQuery(function(){
	
	//获取维护项目页面数据 服务器分页
    var serverItemData = {};
    serverItemData.schemeID=jQuery('#schemeID').val();
    var serverItemTable;
    jQuery(document).ready(function () {
	serverItemTable = jQuery('#resultItemTable').DataTable({
        "serverSide": true,
        "ordering": false,
        "ajax": {
            "dataType": "json",
            "type": "get",
            "cache": false,
            "url":window.rootPath+ '/ui/afterSaleMaintain/getItem',
            "data": function (d) {
                return $.extend({}, d, serverItemData);
            }
        },
        columns: [
            {
            	data: null
            },
            {
	            data: null,
	            defaultContent: "",
	            render: function (data, type, full, meta) {
	            	var id=data.id
	            	var operate ='<input type="checkbox" class="checkchildItem">';
	            	return operate;
	            }   
            },
            {
                data: 'maintainPeriod'
            },
            {
            	data: 'maintainProject'
            },
            {
                data: 'announcement'
            },
            {
                data: 'memo'
            }
            ],
        "drawCallback": function (oSettings) {
            var k = 0;
            var total = oSettings.aiDisplay.length + oSettings._iDisplayStart;

            for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
                $('td:eq(0)', oSettings.aoData[oSettings.aiDisplay[k]].nTr).text(i + 1);
                k++;
            }
        }
    });
	
	////删除维护项目
	jQuery('.delItemBtn').on('click', function () {
		var selects=getSelect("#resultItemTable",1);
		var url=window.rootPath+"/ui/afterSaleMaintain/delItem";
		delSelect(selects,url,serverItemTable)
	});
	
	///新建维护项目，清空弹窗
	jQuery('.addItemBtn').on('click',function(){
		jQuery("#itemId").val('');
        jQuery("#maintainPeriod").val('');
        jQuery("#maintainProject").val('');
        jQuery("#announcement").val('');
        jQuery("#memo").val('');
		jQuery('#maintainItemModal').modal('show');
	});
	
	/////编辑维护项目
    jQuery('.editItemBtn').on('click', function () {
	   var selects=getSelect("#resultItemTable",1);
	   if(selects.length>1){
		   $.springLemonDialog({
		         content: '只能编辑一条记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		   }); 
	   }
	   else if(selects.length==0){
		   $.springLemonDialog({
		         content: '请选择要编辑的记录',// 确认文本
		         clickButton: function (sender, modal, index) {// 按钮事件处理
		             $(this).closeDialog(modal);
		         }
		   }); 
	   }else if(selects.length==1){
		 var itemId=selects[0].id;
		 var maintainPeriod=selects[0].maintainPeriod;
		 var maintainProject=selects[0].maintainProject;
		 var announcement=selects[0].announcement;
		 var memo=selects[0].memo;
		 jQuery("#itemId").val(itemId);
	     jQuery("#maintainPeriod").val(maintainPeriod);
	     jQuery("#maintainProject").val(maintainProject);
	     jQuery("#announcement").val(announcement);
	     jQuery("#memo").val(memo);
	     jQuery('#maintainItemModal').modal('show');
	   }
   	    
   });
	
	//保存维护项目
	jQuery('.saveItemBtn').on('click', function () {
		if(jQuery("#maintainItemAtrrForm") && !jQuery("#maintainItemAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
            return;
        };
        var schemeID=jQuery("#schemeID").val();
        var itemId=jQuery("#itemId").val();
        var maintainPeriod=jQuery("#maintainPeriod").val();
        var maintainProject=jQuery("#maintainProject").val();
        var announcement=jQuery("#announcement").val();
        var memo=jQuery("#memo").val();
        if (schemeID==""){//当前维护方案没有保存
        	if (confirm("当前维护方案还未保存，是否保存？")) {
        	var type=jQuery("#type").val();
            var deviceID=jQuery("#deviceID").val();
            var deviceType=jQuery("#deviceType").val();
            var schemeCode=jQuery("#schemeCode").val();
            var schemeName=jQuery("#schemeName").val();
            var url0=window.rootPath+"/ui/afterSaleMaintain/saveScheme";
            var d = {
            		"type": type,
                    "deviceID": deviceID,
                    "deviceType": deviceType,
                    "schemeCode": schemeCode,
                    "schemeName": schemeName
            };
            jQuery.ajax({
  	          type: 'post',
  	          url: encodeURI(url0),
  	          data: JSON.stringify(d),
  	          dataType: 'json',
  	          contentType: 'application/json;charset=utf-8',
  	          success: function (data) {
  	        	  var schemeId=data.schemeId;
				  jQuery("#schemeID").val(schemeId);
		          var api = window.rootPath + "/api/maintainItems";
		          var data = {
		            		"maintainSchemeID": schemeId,
		                    "maintainPeriod": maintainPeriod,
		                    "maintainProject": maintainProject,
		                    "announcement": announcement,
		                    "memo": memo
		            };
		        	jQuery.ajax({
		                type: 'post',
		                url: api,
		                data: JSON.stringify(data),
		                dataType: 'json',
		                contentType: 'application/json;charset=utf-8',
		                success: function (data) {
		                	jQuery('#maintainItemModal').modal('hide');
		                }
		            });
				  serverItemData.schemeID=schemeId;
				  serverItemTable.draw(true);
  	          }
            });
        	}
        }else{
        	if (itemId==""){//新增
	        	var api = window.rootPath + "/api/maintainItems";
	        	var data = {
	            		"maintainSchemeID": schemeID,
	                    "maintainPeriod": maintainPeriod,
	                    "maintainProject": maintainProject,
	                    "announcement": announcement,
	                    "memo": memo
	            };
	        	jQuery.ajax({
	                type: 'post',
	                url: api,
	                data: JSON.stringify(data),
	                dataType: 'json',
	                contentType: 'application/json;charset=utf-8',
	                success: function (data) {
	                	jQuery('#maintainItemModal').modal('hide');
						serverItemData.schemeID=schemeID;
	                	serverItemTable.draw(true);
	                }
	            });
        	}else{
        		var url = window.rootPath + "/api/maintainItems/" +itemId;
	        	var data = {
	            		"maintainSchemeID": schemeID,
	                    "maintainPeriod": maintainPeriod,
	                    "maintainProject": maintainProject,
	                    "announcement": announcement,
	                    "memo": memo
	            };
	        	jQuery.ajax({
	        		url:encodeURI(url),
	                type:'patch',
	                data: JSON.stringify(data),
	                dataType: 'json',
	                contentType: 'application/json;charset=utf-8',
	                success: function (data) {
	                	jQuery('#maintainItemModal').modal('hide');
	                	serverItemTable.draw(false);
	                }
	            });
        	}
        }
	});

		//保存编辑后的维护方案
		jQuery('.saveEditSchemeBtn').on('click',function(){
			if(jQuery("#maintainSchemeAtrrForm") && !jQuery("#maintainSchemeAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
				return;
			};
			var schemeID=jQuery("#schemeID").val();
			var type=jQuery("#type").val();
			var deviceID=jQuery("#deviceID").val();
			var deviceType=jQuery("#deviceType").val();
			var schemeCode=jQuery("#schemeCode").val();
			var schemeName=jQuery("#schemeName").val();
			var url=window.rootPath+'/api/maintainSchemes/'+schemeID;
			var d = {
				"type": type,
				"deviceID": deviceID,
				"deviceType": deviceType,
				"schemeCode": schemeCode,
				"schemeName": schemeName
			};
			jQuery.ajax({
				url:encodeURI(url),
				type:'patch',
				data: JSON.stringify(d),
				dataType: 'json',
				contentType: 'application/json;charset=utf-8',
				success: function (data) {
					serverItemTable.draw(false);
				}
			});
		});
		//新增保存维护方案
		jQuery('.saveSchemeBtn').on('click', function () {
			if(jQuery("#maintainSchemeAtrrForm") && !jQuery("#maintainSchemeAtrrForm").Validform({tiptype:3, showAllError:true}).check(false)){
				return;
			};
			var schemeID=jQuery("#schemeID").val();
			var type=jQuery("#type").val();
			var deviceID=jQuery("#deviceID").val();
			var deviceType=jQuery("#deviceType").val();
			var schemeCode=jQuery("#schemeCode").val();
			var schemeName=jQuery("#schemeName").val();
			if (schemeID==""){//新增
				var url0=window.rootPath+"/ui/afterSaleMaintain/saveScheme";
				var d = {
					"type": type,
					"deviceID": deviceID,
					"deviceType": deviceType,
					"schemeCode": schemeCode,
					"schemeName": schemeName
				};
				jQuery.ajax({
					type: 'post',
					url: encodeURI(url0),
					data: JSON.stringify(d),
					dataType: 'json',
					contentType: 'application/json;charset=utf-8',
					success: function (data) {
						var schemeId=data.schemeId;
						jQuery("#schemeID").val(schemeId);
						serverItemData.schemeID=schemeId;
						serverItemTable.draw(true);
					}
				});
			}else{//编辑
				var url=window.rootPath+'/api/maintainSchemes/'+schemeID;
				var d = {
					"type": type,
					"deviceID": deviceID,
					"deviceType": deviceType,
					"schemeCode": schemeCode,
					"schemeName": schemeName
				};
				jQuery.ajax({
					url:encodeURI(url),
					type:'patch',
					data: JSON.stringify(d),
					dataType: 'json',
					contentType: 'application/json;charset=utf-8',
					success: function (data) {
						serverItemTable.draw(false);
					}
				});
			}

		});
	//列表中的全选
    $("#checkAllItem").click(function(){
    	var check = $(this).prop("checked");
        $(".checkchildItem").prop("checked", check);
	});
	
	
  });	
});
